module.service('Coin', ['$rootScope', function ($rootScope) {
    var service = {
        _baseUrl: "/azeroth/back/crm/",
        currentItem: {},
        coinList: [],
        totalSceneCount: 0,
        queryList: function (currentPage, pageSize, memberId, memberName, callback) {
            var paramData = {
                memberId: memberId,
                pageNum: currentPage,
                start: (currentPage - 1) * pageSize,
                pageSize: pageSize,
                memberName: memberName,
                needCount: true,
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "queryVAccountBy.htm", paramData, function (resultJson) {
                angular.copy(resultJson.data, service.coinList);
                service.totalSceneCount = resultJson.totalCount;
                if (callback) {
                    callback();
                }
            });
        },
        downItem: function (id, callback) {
            var paramData = {
                id: id
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "downItem.htm", paramData, function (resultJson) {
                //根据id删除掉该数据
                var removeIndex;
                $.each(service.itemList, function (index, result) {
                    if (result['id'] == id) {
                        removeIndex = index;
                    }
                });
                service.itemList.splice(removeIndex, 1);
                if (callback) {
                    callback();
                }
            });
        },
        queryDetail: function (id, callback) {
            var paramData = {
                itemId: id,
                needSku: true
            };
            $rootScope.ajaxRequestPost(service._baseUrl + "getItemDetail.htm", paramData, function (resultJson) {
                angular.copy(resultJson.data, service.currentItem);
                if (callback) {
                    callback();
                }
            });
        },
        insert: function (item, miniSkuList, callback) {
            var paramData = item;
            paramData['miniSkuList'] = JSON.stringify(miniSkuList);

            $rootScope.ajaxRequestPost(service._baseUrl + "publishItem.htm", paramData, function (resultJson) {
                if (callback) {
                    callback();
                }
            });
        },
        update: function (item, miniSkuList, callback) {
            var paramData = item;
            paramData.itemSkuDOList = [];
            paramData.featureValueDOList = [];
            paramData.featureIdList = [];
            paramData['miniSkuList'] = JSON.stringify(miniSkuList);
            $rootScope.ajaxRequestPost(service._baseUrl + "updateItem.htm", paramData, function (resultJson) {
                if (callback) {
                    callback();
                }
            });
        }
    };
    return service;
}]);